<?php
declare (strict_types = 1);

namespace app\common\service;

use app\common\model\StudyStudentAction;
use app\common\model\StudyStudentLeave;
use app\common\model\StudyStudentList;
use think\facade\Request;
use app\common\model\StudentActionAttendance as M;
use app\common\validate\StudentActionAttendance as V;

class StudentActionAttendance
{
    // 添加
    public static function goAdd($data)
    {
      if (\request()->isAjax()) {
          $student_id = input('param.student_id', '');
          if ($student_id) {
              $student_id = input('param.student_id');
              $student[] = $student_id;
          } else {
              $class_id = input('param.class_id');
              $child = StudyStudentList::where('class_id', $class_id)->where('end_time', '>=', time())->select();
              $student = [];
              foreach ($child as $key => $value) {
                  $is_leave = StudyStudentLeave::where('student_id', $value['id'])
                      ->where('start_time', '<', time())
                      ->where('end_time', '>', time())
                      ->find();
                  if (!$is_leave) {
                      $student[] = $value['id'];
                  }
              }
          }

          $admin_id = input('param.admin_id', 0);
          $add_time = input('param.add_time', '');
          $action_info = input('param.action_info', '');
          $shuttle_type = input('param.shuttle_type', '');
          $shuttle_img = input('param.shuttle_img', '');
          $attendance_type = input('param.attendance_type', '到校');
          foreach ($student as $v) {
              $id = \app\common\model\StudentActionAttendance::insertGetId([
                  'student_id' => $v,
                  'add_time' => strtotime($add_time),
                  'admin_id' => $admin_id,
                  'action_info' => $action_info,
                  'shuttle_type' => $shuttle_type,
                  'shuttle_img' => $shuttle_img,
                  'attendance_type' => $attendance_type,
              ]);
              if ($attendance_type == '到校') {
                  StudyStudentAction::create([
                      'student_id' => $v,
                      'action_type' => 11,
                      'action_id' => $id,
                      'add_time' => time(),
                      'admin_id' => $admin_id,
                  ]);
              } else {
                  StudyStudentAction::create([
                      'student_id' => $v,
                      'action_type' => 12,
                      'action_id' => $id,
                      'add_time' => time(),
                      'admin_id' => $admin_id,
                  ]);
              }
          }


      }
    }
    
    // 编辑
    public static function goEdit($data,$id)
    {
        $data['id'] = $id;
        //验证
        $validate = new V;
        if(!$validate->scene('edit')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
             M::update($data);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 状态
    public static function goStatus($data,$id)
    {
        $model =  M::find($id);
        if ($model->isEmpty())  return ['msg'=>'数据不存在','code'=>201];
        try{
            $model->save([
                'status' => $data,
            ]);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 删除
    public static function goRemove($id)
    {
        $model = M::find($id);
        if ($model->isEmpty()) return ['msg'=>'数据不存在','code'=>201];
        try{
           $model->delete();
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 批量删除
    public static function goBatchRemove($ids)
    {
        if (!is_array($ids)) return ['msg'=>'数据不存在','code'=>201];
        try{
            M::destroy($ids);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 获取列表
    public static function goRecycle()
    {
        if (Request::isPost()){
            $ids = Request::param('ids');
            if (!is_array($ids)) return ['msg'=>'参数错误','code'=>'201'];
            try{
                if(Request::param('type')){
                    $data = M::onlyTrashed()->whereIn('id', $ids)->select();
                    foreach($data as $k){
                        $k->restore();
                    }
                }else{
                    M::destroy($ids,true);
                }
            }catch (\Exception $e){
                return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
            }
            return ['msg'=>'操作成功'];
        }
        //按用户名
        $where = [];
        $limit = input('get.limit');
        
        $list = M::onlyTrashed()->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }
}
